package com.topcoder.srm516;

import java.util.HashSet;


public class NetworkXOneTimePad {
	public int crack(String[] plaintexts, String[] ciphertexts) {
		HashSet<Long> plain = convert(plaintexts);
		HashSet<Long> cipher = convert(ciphertexts);
		int res = 0;
		long c0 = cipher.iterator().next();
		
		for (long p : plain) {
			long key = p ^ c0;
			boolean ok = true;
			for (long c : cipher) {
				if (!plain.contains(c^key)) {
					ok = false;
					break;
				}
			}
			if (ok) res++;
		}
		
		return res;
	}
	
	private HashSet<Long> convert(String[] message) {
		HashSet<Long> a = new HashSet<Long>();
		for (int i=0; i<message.length; i++) a.add(Long.parseLong(message[i], 2));
		return a;
	}
}
